package com.baselibrary.utils;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

/**
 * 首页瀑布流间距
 */
public class StaggeredItemDecorationNew extends RecyclerView.ItemDecoration {
    private Context context;
    private float interval;

    /**
     *
     */
    public StaggeredItemDecorationNew(Context context) {
        this.context = context;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, RecyclerView parent, @NonNull RecyclerView.State state) {
        setOutRect(outRect, view);
    }

    private void setOutRect(@NonNull Rect outRect, @NonNull View view) {
        StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
        // 获取item在span中的下标
        if (params.getViewAdapterPosition() > 0) {
            int spanIndex = params.getSpanIndex();
            if (spanIndex % 2 == 0) {
                outRect.right = ScreenUtils.dpToPx(2.5f);
                outRect.left = ScreenUtils.dpToPx(14);
            } else {
                outRect.right = ScreenUtils.dpToPx(14);
                outRect.left = ScreenUtils.dpToPx(2.5f);
            }
            // 上方间隔
            outRect.bottom = ScreenUtils.dpToPx(5);
        }
    }
}
